<?php
	class Controller_Fulfillment extends Controller_Abstract{
		
		function actionIndex(){
			if(isset($_SESSION['infos'])){
				$this->_view['infos'] = $_SESSION['infos'];
				unset($_SESSION['infos']);
			}
			if(isset($_SESSION['errors'])){
				$this->_view['erros'] = $_SESSION['errors'];
				unset($_SESSION['erros']);
			}
			$page = $this->_context->get('page', 1);
			$field = $this->_context->get('field', null);
			$condition = $this->_context->get('condition', null);
			$onlyPending = true;
			if($field !== null){
				$onlyPending = false;
			}
			
			$fmodel = new Fulfillment();
			$fulfillments = $fmodel->search($field, $condition, $page, $onlyPending);
			if(isset($fulfillments['totalCount'])){
				$pagination = new Pagination($fulfillments['totalCount'], $this->_context->requestUri(), $page);
				unset($fulfillments['totalCount']);
			}
			
			if(!empty($fulfillments)){
				foreach($fulfillments as $fulfillment){
						$userinfos[]=$fmodel->getUserInfo($fulfillment['uid']);
				}
				$this->_view['userinfos'] = $userinfos;
			}
			if(isset($_SESSION['hash'])){
				$this->_view['hash'] = $_SESSION['hash'];
				unset($_SESSION['hash']);
			}
			$this->_view['field'] = $field;
			$this->_view['condition'] = $condition;
			$this->_view['pagination'] = $pagination->render();
			$this->_view['fulfillments'] = $fulfillments;
		}
		
		function actionCancel(){
			$fid = $this->_context->fid;
			$_SESSION['hash'] = $this->_context->hash;
			$fmodel = new Fulfillment();
			if($fmodel->cancel($fid)){
				$infos = array();
				$infos[] = Util::getMessage(9001);
				$_SESSION['infos'] = $infos;
			}else{
				$errors = array();
				$errors[] = Util::getMessage(9002);
				$_SESSION['erros'] = $errors;
			}
			return $this->_redirect(url('fulfillment/index'));
		}
		
		function actionComplete(){
			$fid = $this->_context->fid;
			$_SESSION['hash'] = $this->_context->hash;
			$fmodel = new Fulfillment();
			if($fmodel->complete($fid)){
				$infos = array();
				$infos[] = Util::getMessage(9003);
				$_SESSION['infos'] = $infos;
			}else{
				$errors = array();
				$errors[] = Util::getMessage(9002);
				$_SESSION['erros'] = $errors;
			}
			return $this->_redirect(url('fulfillment/index'));
		}
		
		function actionCheckreturned(){
			$fid = $this->_context->fid;
			$_SESSION['hash'] = $this->_context->hash;
			$fmodel = new Fulfillment();
			if($fmodel->checkReturned($fid)){
				$infos = array();
				$infos[] = Util::getMessage(9004);
				$_SESSION['infos'] = $infos;
			}else{
				$errors = array();
				$errors[] = Util::getMessage(9002);
				$_SESSION['erros'] = $errors;
			}
			return $this->_redirect(url('fulfillment/index'));
		}
		
		function actionFraudulent(){
			$uid = $this->_context->uid;
			$_SESSION['hash'] = $uid;
			if($uid){
				$userModel = new User();
				$userInfo = $userModel->getUserByField('id', $uid);
				$siteModel = new Site();
				$siteInfo = $siteModel->getSiteById($userInfo['site_id']);
				$token = $userModel->updateToken($userInfo['id']);
				
				/**
				 * update users unsub=1
				 * @author mason
				 * @since 2012-5-9
				 */
				if($userInfo['unsub'] != 1){
					$unsub = $userModel->updateUnsub($userInfo['id']);
				}
				
				if($token){
					if(!Util::addNewEmail('fraudulent', $siteInfo['template'], array('email'=>$userInfo['email'], 'firstname'=>$userInfo['firstname'], 
									  'domain'=>$siteInfo['domain'], 'name' => $siteInfo['name'], 'uid'=>$userInfo['id'], 'priority'=>50))){
						$errors = array();
						$errors[] = Util::getMessage(9002);
						$_SESSION['erros'] = $errors;
				  	}
				  	$infos = array();
					$infos[] = Util::getMessage(9005);
					$_SESSION['infos'] = $infos;
				}
			}
			return $this->_redirect(url('fulfillment/index'));
		}
	}